const BaiduMapMixin = {
  methods: {
    /**
     * 初始化百度地图
     * @param {String} containerId 地图容器Id
     */
    initBaiduMap({ containerId, lng, lat }) {
      let map = (this.__baiduMap__ = new BMap.Map(containerId))

      // map.addEventListener('tilesloaded', function (e) {
      // 在地图加载完成后执行的代码
      console.log('地图加载完成')
      if (lng !== '' && lat !== '') {
        let point = new BMap.Point(lng, lat)
        this.addPoint(point)
        map.centerAndZoom(point, 15)
      }

      map.addControl(new BMap.MapTypeControl()) // 添加地图类型控件
      map.enableScrollWheelZoom(true) // 开启鼠标滚轮缩放

      return this
      // })
    },

    clearOverlays() {
      this.__baiduMap__.clearOverlays()
    },

    addPoint(point) {
      this.__baiduMap__.addOverlay(new BMap.Marker(point))
    },

    getBaiduMap() {
      return this.__baiduMap__
    },

    /**
     * 绑定地图点击事件
     * @param {Function} callback 点击回调
     */
    bindMapClickEvent(callback) {
      let map = this.__baiduMap__
      map.addEventListener('click', function (e) {
        map.clearOverlays()
        let point = new BMap.Point(e.point.lng, e.point.lat)
        map.addOverlay(new BMap.Marker(point))
        // 触发回调
        callback && callback(e)
      })
    },
  },
}

export default BaiduMapMixin
